package com.xrc.service;

import com.spring.BeanPostProcess;
import com.spring.Component;
import com.spring.ComponentScan;
import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * @ClassName:TestBeanPostProcess
 * @Auther: xierongchang
 * @Description:
 * @Date: 2024/6/11 22:49
 * @Version: v1.0.0
 */
@Component
public class TestBeanPostProcess implements BeanPostProcess {


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if("userService".equals(beanName)){
           Object proxy = Proxy.newProxyInstance(TestBeanPostProcess.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                    System.out.println("切面");
                    return method.invoke(bean,objects);
                }
            });
           return proxy;
        }
        System.out.println("before ");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println("after ");
        return BeanPostProcess.super.postProcessAfterInitialization(bean, beanName);
    }
}
